package com.yjd.bi.statistic.control;

import com.yjd.bi.comm.utils.BiDateUtil;
import com.yjd.bi.report.enums.TimeType;
import com.yjd.bi.report.model.EChartsModel;
import com.yjd.bi.user.service.IUserStatisticsConditionService;
import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.StringUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
@Scope("prototype")
@RequestMapping("/sex/")
public class SexControl extends BaseControl {

    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private IUserStatisticsConditionService userStatisticsConditionService;

	@RequestMapping("index")
	public String deal() throws Exception {
        request.setAttribute("start", "2013-12-21");
        request.setAttribute("now", BiDateUtil.initBiDate());
        return "sex.index";
	}

    @RequestMapping("getSummary")
    public void getIndexData(String start,String end,String condition,String field) throws Exception {
		try{
			start=getTime(start);
			end=getTime(end);
		}catch(Exception e){
			this.errorMsg("参数有误！");
			return;
		}
		if(StringUtil.isEmpty(condition)||StringUtil.isEmpty(field)){
			this.errorMsg("参数有误！");
			return;
		}
		Map paramMap = FrameUtil.newHashMap("start",DateUtil.dateStringToTimestamp(start, DateUtil.PATTERN_yyyyMMdd),"end",DateUtil.dateStringToTimestamp(end, DateUtil.PATTERN_yyyyMMdd)+86399,"condition",condition,"propertyName", field);
		this.successData(FrameUtil.newHashMap("summaryVals",userStatisticsConditionService.getSummary(paramMap)));
	}
	
	/**
	 * 根据日期获取时段查询统计性别相关数据
	 * @param timeType
	 * @param timeFrom
	 * @throws Exception
	 */
	@RequestMapping("getDatasByField")
	public void getDatasByField(String timeType,String timeFrom,String field,String sex) throws Exception{
		try{
			timeFrom=DateUtil.timestampToDateString(DateUtil.dateStringToTimestamp(timeFrom, DateUtil.PATTERN_yyyy_MM_dd),"yyyyMMdd");
			TimeType.getType(Integer.parseInt(timeType));
			Long.parseLong(timeFrom);
		}catch(Exception e){
			this.errorMsg("参数有误！");
		}
		TimeType t=TimeType.getType(Integer.parseInt(timeType));
		EChartsModel ec=this.userStatisticsConditionService.getDates(t,Integer.parseInt(sex),Long.parseLong(timeFrom), field);
		this.successData(FrameUtil.newHashMap("option",ec));
	}
	
	/**
	 * 根据yyyy-MM-dd转为long格式的yyyyMMdd
	 * @return
	 */
	public String getTime(String time){
		return DateUtil.timestampToDateString(DateUtil.dateStringToTimestamp(time, DateUtil.PATTERN_yyyy_MM_dd), DateUtil.PATTERN_yyyyMMdd);
	}
}
